﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Providers.LiveControl.Server;
using Color = System.Windows.Media.Color;

namespace 截屏测试
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            
        }

      

      

        protected virtual void Heartbeat()
        {
           
        }

        public event EventHandler<EventArgs> OnInvalidateVisual;
        readonly ScreenShotHelper _screenShotHelper = new ScreenShotHelper();
        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            try
            {
                var rectangle = new Rect(new System.Windows.Point(0, 0), this.RenderSize);
                _screenShotHelper.ScreenShot();


                var image = _screenShotHelper[0];
                var bitMapImage = BitmapToBitmapImage(image);
                drawingContext.DrawImage(bitMapImage, rectangle);
                
            }
            catch (Exception)
            {
                
            }
            
        }
        public BitmapImage BitmapToBitmapImage(Bitmap bitmap)
        {
            Bitmap bitmapSource = new Bitmap(bitmap.Width, bitmap.Height);
            int i, j;
            for (i = 0; i < bitmap.Width; i++)
                for (j = 0; j < bitmap.Height; j++)
                {
                    System.Drawing.Color pixelColor = bitmap.GetPixel(i, j);
                    System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B);
                    bitmapSource.SetPixel(i, j, newColor);
                }
            MemoryStream ms = new MemoryStream();
            bitmapSource.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = new MemoryStream(ms.ToArray());
            bitmapImage.EndInit();

            return bitmapImage;
        }
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            StartApploop();
        }

        private Thread _mLoopThread;

        protected void StartApploop()
        {

            _mLoopThread = new Thread(Loop)
            {
                Name = "",
                IsBackground = true
            };
            _mLoopThread.Start();

        }

        private bool _flagRun;

      

        private void Loop()
        {
            _flagRun = true;
            do
            {
                //try
                //{
                Heartbeat();
                //}
                //catch (Exception)
                //{
                //    // ignored
                //}
            } while (_flagRun);
        }

        public void StopLoop()
        {
            _flagRun = false;
        }
    }
}
